package com.guess.lolguessing.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guess.lolguessing.context.BaseContext;
import com.guess.lolguessing.domain.dto.CommentDTO;
import com.guess.lolguessing.domain.po.Comment;
import com.guess.lolguessing.domain.queryPage.CommentPage;
import com.guess.lolguessing.domain.vo.CommentVO;
import com.guess.lolguessing.mapper.CommentMapper;
import com.guess.lolguessing.service.ICommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2024-10-18
 */
@Service
@RequiredArgsConstructor
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements ICommentService {

    private final StarCommentServiceImpl starCommentService;

    private final KpUserServiceImpl userService;
    @Override
    public void submitComment(CommentDTO dto) {
        Comment comment = BeanUtil.copyProperties(dto, Comment.class);
        comment.setUserId(BaseContext.getCurrentId().intValue());
        this.save(comment);
        if(dto.getRating() != 0) {
            starCommentService.updateRating(dto.getMatchId(), dto.getPlayerId(), dto.getRating());
        }
    }

    public CommentPage getComments(Integer matchId, Integer playerId, Integer page) {
        int pageSize = 6;

        // 创建分页对象
        Page<Comment> commentPage = new Page<>(page, pageSize);

        // 分页查询
        IPage<Comment> resultPage = this.lambdaQuery()
                .eq(Comment::getMatchId, matchId)
                .eq(Comment::getPlayerId, playerId)
                .page(commentPage);

        // 获取查询结果中的评论列表
        List<Comment> comments = resultPage.getRecords();
        System.out.println(comments.size());
        // 构造返回的 VO 列表
        List<CommentVO> voList = new ArrayList<>();

        // 遍历查询到的评论列表，并通过 userId 查找 username，构造 CommentVO
        for (Comment comment : comments) {
            CommentVO vo = new CommentVO();
            vo.setRating(comment.getRating().intValue());

            vo.setMsg(comment.getComment());

            // 通过 userId 查询对应的用户名
            String username = userService.getUsernameById(comment.getUserId());
            vo.setUsername(username);

            voList.add(vo);
        }
        CommentPage commentPage1 = new CommentPage();
        commentPage1.setComments(voList);
        commentPage1.setTotalPageSize((int) resultPage.getPages());
        return commentPage1;
    }

    @Override
    public int findNotZeroNumber(Integer matchId, Integer playerId) {
        List<Comment> list = this.lambdaQuery().eq(Comment::getMatchId, matchId)
                .eq(Comment::getPlayerId, playerId)
                .ne(Comment::getComment ,0).list();
        return list.size();
    }
}
